package com.orangelife.utils;

import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCountCallbackHandler;

import com.orangelife.exception.AppBizException;

public class TableColumn {

	/**
	 * 获取某个表的所有字段
	 * 
	 * @param jdbcTemplate
	 * @param tableName
	 * @param datasourceType
	 * @return
	 */
	public List<String> getColumnName(JdbcTemplate jdbcTemplate,
			String tableName, String datasourceType) {
		StringBuffer buffer = new StringBuffer();
		if (datasourceType.toUpperCase().equals("MYSQL")) {
			buffer.append("  LIMIT  1");
		} else if (datasourceType.toUpperCase().equals("ORACLE")) {
			buffer.append("   WHERE ROWNUM <=1");
		}

		String sql = "select * from " + tableName;
		List<String> list = new ArrayList<String>();
		RowCountCallbackHandler rcch = new RowCountCallbackHandler();
		try {
			jdbcTemplate.query(sql, rcch);
			String coloumn[] = rcch.getColumnNames();
			for (int i = 0; i < coloumn.length; i++) {
				list.add(coloumn[i].toString());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	/**
	 * 判断传入参数是否等于字段
	 * 
	 * @param jdbcTemplate
	 * @param tableName
	 * @param datasourceType
	 * @param parameter
	 * @return
	 */
	public boolean isEqual(JdbcTemplate jdbcTemplate, String tableName,
			String datasourceType, String parameter) {
		boolean flag = false;
		List<String> columnclist = this.getColumnName(jdbcTemplate, tableName,
				datasourceType);
		if (columnclist != null && columnclist.size() > 0) {
			for (String columnc : columnclist) {
				if (columnc.toLowerCase().equals(parameter.toLowerCase())) {
					flag = true;
				}
			}
		}
		return flag;
	}

	public String getOrderbyType(JdbcTemplate jdbcTemplate, String tableName,
			String datasourceType, String parameter, String defaltSql)
			throws AppBizException {
		StringBuffer buffer = new StringBuffer();

		if (parameter == null) {
			buffer.append(defaltSql);
		} else {
			String para[] = parameter.split(",");

			if (para != null && para.length > 0) {
				for (int i = 0; i < para.length; i++) {
					String parametertTemp = "";
					if (para[i].trim().substring(0, 1).equals("+")
							|| para[i].trim().substring(0, 1).equals("-")) {
						parametertTemp = para[i].substring(1, para[i].length());
					} else {
						throw new AppBizException(000003, "参数格式不正确");
					}
					if (this.isEqual(jdbcTemplate, tableName, datasourceType,
							parametertTemp)) {
						if (para[i].trim().substring(0, 1).equals("-")) {
							if (i == 0) {
								buffer.append("    order by   "
										+ parametertTemp + "   desc ");
							} else {
								buffer.append("   ,   " + parametertTemp
										+ "   desc ");
							}
						} else if (para[i].trim().substring(0, 1).equals("+")) {
							if (i == 0) {
								buffer.append("   order by    "
										+ parametertTemp + "  asc ");
							} else {
								buffer.append("   ,  " + parametertTemp
										+ "  asc ");
							}
						}
					} else {
						return buffer.toString();
					}

				}

			}

		}
		return buffer.toString();
	}
}
